/*
 * Copyright (c) 2012, Tony Houghton <h@realh.co.uk>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

// HGame - a simple cross-platform game framework

#include "handroid/Application.h"
#include "handroid/GLRenderContext.h"
#include "handroid/Platform.h"
#include "handroid/Thread.h"

namespace handroid {

int RenderRunnable::runSafely()
{
    mApplication->renderLoop();
    return 0;
}

Application::Application(struct android_app *aapp) :
        hgame::Application(new Platform(aapp, "Bombz"),
                "AndroidApp", new ThreadFactory()),
        mAndroidApp(aapp),
        mRenderRunnable(this),
        mRenderThread(0)
{
}

Application::~Application()
{
    delete mRenderThread;
}

void Application::start()
{
    mState->userData = reinterpret_cast<void *>(this);
    mState->onAppCmd = handleCmd;
    mState->onInputEvent = handleInput;
    /*
    assert(mScreen != 0);
    try {
        createRenderContext();
        mScreen->requestRenderState(hgame::Screen::RENDER_STATE_RENDERING);
    }
    catch (std::exception &e)
    {
        mLog.f("Exception initialising render context: %s", e.what());
        std::exit(1);
    }
    mEventThread->start();
    mScreenThread->start();
    renderLoop();
    delete mRenderContext;
    mRenderContext = 0;
    mScreenThread->wait();
    mEventThread->wait();
    */
}

void Application::handleCmd(struct android_app *app, int32_t cmd)
{
    Application *that = reinterpret_cast<Application *>(app);
}

int32_t Application::handleInput(struct android_app *app, AInputEvent* event)
{
    return 0;
}

void Application::createRenderContext()
{
    mRenderContext = new GLRenderContext(mScreen->getBestModes());
}

void Application::stop()
{
    pushEvent(new hgame::Event(hgame::EVENT_STOP, true));
    mScreenRunnable.stop();
    mRenderRunnable.stop();
    hgame::Application::stop();
}

}

